GINO Graphics Suite - GINO v9.0  

Depth Buffering

Once the depth buffer has been enabled using one of the shading modes described above, the decision to display an object is based on two things. 1) The distance of the object in relation to other objects already on the screen or window and 2) the logical depth test to be applied. By default these are calculated as follows:

The distance of an object from the viewer is mapped on to a value between 0.0 and 1.0 which represents the total range of the viewing volume as set up by gSetViewport3D(). Therefore objects at the back of the volume (i.e. furthest away from the viewer) are said to have a depth of 1.0 and objects closes to the viewer are said to have a depth of 0.0. When the screen or window is cleared the depth buffer is set to 1.0 for each pixel. Whenever a new object is displayed, its distance from the viewer is mapped onto the depth range (0.0 to 1.0) and for each pixel, if the depth is less than the depth in the depth buffer, the pixel is displayed and the buffer is updated. Otherwise if the depth is greater than or equal to the value in the depth buffer the pixel is not displayed (because it is deemed to behind an existing object).

In most systems, this depth range (0.0 to 1.0) is further mapped onto a 32bit or 16 bit integer to speed up the calculation and so there are in fact only 2**16 or 2**32 different depth values that can be tested against. This trade off of performance against accuracy enhances the importance of the correct setting of the viewing volume as set up by gSetViewport3D(), because too large a range will decrease the accuracy of the depth buffering algorithms. This can result in small objects which lie in front of existing objects not being displayed because their depth (as a 16/32bit integer) is calculated as being the same as the existing object.

It is possible to change these default settings using the following routine:

gSetDepthMode(mode,dinit)

where mode is the test that is applied to each pixel to be displayed, against the value in the depth buffer. The list of possible modes is given in the table below, together with its normal setting of the depth buffer initial value, dinit:

Mode Initial Value
GNEVER n/a
GLESSTHAN (default) 1.0
GLESSTHANOREQUALTO 1.0
GEQUALTO As required
GNOTEQUALTO As required
GGREATERTHANOREQUALTO 0.0
GGREATERTHAN 0.0
GALWAYS n/a

As explained above, the default mode is GLESSTHAN and most of the alternative settings would only be required when special effects are required. Setting the mode to GLESSTHANOREQUAL can be useful for adding detail onto the surface of an existing object as this sets the logical test so that pixels less than or equal to the existing objects are still displayed. However this technique depends on the accuracy of the Z range set in the current 3D viewport (see also gSetFacetOffsetMode() for an alternative method).

Note that the depth buffer is only initialized to the value set in dinit when the screen is next cleared (gNewDrawing()) and mode is activated when gSetShadingMode() is next called.

The current depth buffer mode settings can be enquired using the following routine:

gEnqDepthMode(mode,dinit)

where the arguments return the values last set by gSetDepthMode() or the default ones.